package com.zk.iotgateway.service.impl;

import com.zk.iotgateway.manager.GlobalManager;
import com.zk.iotgateway.dao.IotConfigMapper;
import com.zk.iotgateway.domain.param.IotConfigDO;
import com.zk.iotgateway.net.tcp.IotNettyTcpServerService;
import com.zk.iotgateway.net.tcp.IotTcpServerChannelInitializer;
import com.zk.iotgateway.netty.server.ServerForTerminal;
import com.zk.iotgateway.service.IServerService;
import com.zk.iotgateway.service.IotConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * ${DESCRIPTION}
 *
 * @author:mn.m
 * @create 2019-05-04 下午 09:01
 **/
@Service
@Transactional
public class IotConfigServiceImpl  implements IotConfigService {

    @Resource
    private  IotConfigMapper iotConfigMapper;
    @Autowired
    private IotTcpServerChannelInitializer initializer;


    @Override
    public List findAll() {
        return iotConfigMapper.listAll();
    }

    @Override
    public List findAllValid() {
        return iotConfigMapper.listAllValid();
    }

    @Override
    public void addConfig(IotConfigDO iotConfigDO) {
        iotConfigMapper.insertSelective(iotConfigDO);
    }

    @Override
    public void editConfig(IotConfigDO iotConfigDO) {
        iotConfigMapper.updateByPrimaryKeySelective(iotConfigDO);
    }

    @Override
    public void removeConfg(String port) {
        iotConfigMapper.deleteByPrimaryKey(port);
    }

    @Override
    public IotConfigDO findConfigByPort(String port) {
        return iotConfigMapper.selectByPrimaryKey(port);
    }

    @Override
    public boolean establish(IotConfigDO iotConfigDO) {

        if (iotConfigDO!=null){
            new Thread(new Runnable() {
                public void run() {
                    int work=Integer.parseInt(iotConfigDO.getWork());
                    int boss=Integer.parseInt(iotConfigDO.getBoss());
                    long alladle=Long.parseLong(iotConfigDO.getAllIdleTime());
                    initializer.setAllIdleTime(alladle);
                    IotNettyTcpServerService iotNettyTcpServerService = new IotNettyTcpServerService(iotConfigDO.getPort(),boss,work,initializer);
                    iotNettyTcpServerService.startService();
                }
            }).start();
            System.out.println("-------iotgateway----------");
            iotConfigDO.setIsOpen("1");
            this.iotConfigMapper.updateByPrimaryKeySelective(iotConfigDO);
            return true;
        }
        return false;
    }

    @Override
    public boolean shutdown(String port) {
        try{
            if (!org.springframework.util.StringUtils.isEmpty(port)){
                IServerService service = GlobalManager.getInstance().getService(port);
                if (service==null){
                    return false;
                }
                service.stopService();
                System.out.println("-----------------");
                IotConfigDO iotConfigDO = this.iotConfigMapper.selectByPrimaryKey(port);
                iotConfigDO.setIsOpen("0");
                this.iotConfigMapper.updateByPrimaryKeySelective(iotConfigDO);
                GlobalManager.getInstance().removeService(port);
                return true;
            }
            return false;
        }catch (Exception e){
            e.printStackTrace();
            return  false;
        }
    }

}
